﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.Net;
using CYTS.Aoyou.Framework.Performance.MVCPerformance;

namespace CYTS.Aoyou.Framework.Web
{
    /// <summary>
    /// action拦截器
    /// </summary>
    [Obsolete]
    public class ActionPerformanceFilterAttribute : FilterAttribute, IActionFilter
    {
        private MVCPerformanceEntity mvcEntity;
        private MVCPerformanceHelper mvcHelper;
        /// <summary>
        /// 执行action前
        /// </summary>
        /// <param name="filterContext"></param>
        [Obsolete]
        public void OnActionExecuting(ActionExecutingContext filterContext)
        {
            try
            {
                mvcEntity = new MVCPerformanceEntity();
                mvcEntity.ClientAddress = filterContext.RequestContext.HttpContext.Request.UserHostAddress;
                mvcEntity.ServerName = Dns.GetHostName();
                mvcEntity.ExecuteFunName = filterContext.ActionDescriptor.ControllerDescriptor.ControllerName + "/" + filterContext.ActionDescriptor.ActionName;
                mvcEntity.ServiceType = "MVCPerformance";
                mvcEntity.ServerAddress = (Dns.GetHostAddresses(mvcEntity.ServerName))[1].ToString();
                mvcEntity.InterFaceName = filterContext.RequestContext.HttpContext.Request.Url.Authority;
                mvcHelper = new MVCPerformanceHelper();
            }
            catch
            {
                mvcEntity = null;
            }
        }

        /// <summary>
        /// 执行action后
        /// </summary>
        /// <param name="filterContext"></param>
        [Obsolete]
        public void OnActionExecuted(ActionExecutedContext filterContext)
        {
            try
            {
                if (mvcEntity != null)
                {
                    mvcHelper.SetData(mvcEntity);
                }
            }
            catch
            {
            }
        }
    }

    /// <summary>
    /// 异常拦截器
    /// </summary>
    [Obsolete]
    public class ExceptionFilterAttribute : FilterAttribute, IExceptionFilter
    {
        [Obsolete]
        void IExceptionFilter.OnException(ExceptionContext filterContext)
        {
            filterContext.Controller.ViewData["ErrorMessage"] = filterContext.Exception.Message;
            filterContext.Result = new ViewResult()
            {
                ViewName = "Error",
                ViewData = filterContext.Controller.ViewData
            };
            filterContext.ExceptionHandled = true;
        }
    }

    /// <summary>
    /// result拦截器
    /// </summary>
    [Obsolete]
    public class ResultFilterAttribute : FilterAttribute, IResultFilter
    {
        /// <summary>
        /// 执行页面后触发
        /// </summary>
        /// <param name="filterContext"></param>
        [Obsolete]
        public void OnResultExecuted(ResultExecutedContext filterContext)
        {
            filterContext.RequestContext.HttpContext.Response.Write("Result已经执行了!");
        }

        /// <summary>
        /// 执行页面前触发
        /// </summary>
        /// <param name="filterContext"></param>
        [Obsolete]
        public void OnResultExecuting(ResultExecutingContext filterContext)
        {
            filterContext.RequestContext.HttpContext.Response.Write("Result执行之前!");
        }
    }
}
